﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Documents;

namespace Zero.UControl.Demo.Views.UAdorner.Component
{
    public static class ElementDecoratorManager
    {
        public static void ApplyAll(IElementDecorator[] decorators, UIElement element,Canvas canvas)
        {
            foreach (var decorator in decorators)
            {
                decorator.Attach(element, canvas);
            }
        }

        public static void RemoveAll(UIElement element)
        {
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(element);
            if (adornerLayer == null) return;

            var adorners = adornerLayer.GetAdorners(element);
            if (adorners == null) return;

            Type type = typeof(ElementDecoratorBase);
            foreach (var adorner in adorners)
            {
                if(type.IsAssignableFrom(adorner.GetType()))
                    ((ElementDecoratorBase)adorner).Dettach(element);
                adornerLayer.Remove(adorner);
            }
        }

        public static void ShowAll(UIElement element)
        {
            var adorners = GetAdorners(element);
            if (adorners == null) return;

            foreach (var adorner in adorners)
            {
                adorner.Visibility = Visibility.Visible;
            }
        }

        public static void HideAll(UIElement element)
        {
            var adorners = GetAdorners(element);
            if (adorners == null) return;

            foreach (var adorner in adorners)
            {
                adorner.Visibility = Visibility.Collapsed;
            }
        }

        private static Adorner[]? GetAdorners(UIElement element)
        {
            var adornerLayer = AdornerLayer.GetAdornerLayer(element);
            return adornerLayer?.GetAdorners(element);
        }
    }
}
